import base64
import gzip
import io
import json
import random
import ssl
import time
import requests
from websocket import create_connection
from other.quantify import *  #BB_URL, USER_AGENT_LIST, IP_LIST
from sql.sql_conn import Mysql_data


def bb_conn():
    mm = 0
    while True:
        try:
            data_1 = {
                "command": "request_session",
                "params": {
                    "language": "zhh",
                    "site_id": "510",
                    "release_date": "10/07/2024-11:30"
                },
                "rid": int(time.time() * 10000)
            }
            bb_headers = {
                'Accept-Encoding': 'gzip, deflate, br',
                'Accept-Language': 'zh,zh-CN;q=0.9',
                'Cache-Control': 'no-cache',
                'Connection': 'Upgrade',
                # 'Host': 'ws.quickintegrationbest.net',
                # 'Origin': 'https://web4.quickintegrationbest.net',
                'Pragma': 'no-cache',
                'Sec-Websocket-Extensions': 'permessage-deflate; client_max_window_bits',
                'Sec-Websocket-Key': 'LGbJdZVHqZScFMFG/YDVIw==',
                'Sec-Websocket-Version': '13',
                'Upgrade': 'websocket',
                # 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
                "User-Agent": random.choice(USER_AGENT_LIST),
            }
            ws = create_connection(url=BB_URL, timeout=1, headers=bb_headers, sslopt={"cert_reqs": ssl.CERT_NONE}, )
            #http_proxy_host = "60.28.200.218", http_proxy_port = "3612", proxy_type = "http")
            ws.send(json.dumps(data_1))
            a = ws.recv()
            # print(a)
            return ws
        except Exception as e:
            mm += 1
            print('BB连接异常：', e)
            print("BB正在重新连接...")
            if mm == 2:
                print("退出重新连接")
                return None
            continue


# 113.228.89.67:4231
# 111.72.200.158:4256
# 117.42.94.2:4212
# 222.90.4.59:4214
# 180.105.218.132:4215
def fb_connect(url, json_data, ss):
    mm = 0
    while True:
        try:
            fb_headers = {

                'Accept-Encoding': 'gzip, deflate, br',
                'Accept-Language': 'zh-CN,zh;q=0.9',
                # 'Authorization': 'tt_Kun95G2mMk4daYgbsxdBoY7CXIuZjf3f.2f1e510c6e09ec743ecde0d11e8abe0a',keep-alive
                'Connection': 'close',
                'Content-Length': '40',
                'Content-Type': 'application/json;charset=UTF-8',
                # 'Host': 'iapi.ccapykdsd.com',
                # 'Origin': 'https://comwspc.chengxintech9.com',
                # 'Referer': 'https://comwspc.chengxintech9.com/',
                # 'sec-ch-ua': '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"',
                # 'sec-ch-ua-mobile': '?0',
                # 'sec-ch-ua-platform': '"Windows"',
                # 'Sec-Fetch-Dest': 'empty',
                # 'Sec-Fetch-Mode': 'cors',
                # 'Sec-Fetch-Site': 'cross-site',
                'User-Agent': random.choice(USER_AGENT_LIST)
            }
            fb_headers1 = {
                'Accept': 'application/json, text/plain, */*',
                'Accept-Encoding': 'gzip, deflate, br',
                'Accept-Language': 'zh-CN,zh;q=0.9',
                'Connection': 'keep-alive',
                'Content-Length': '40',
                'Content-Type': 'application/json;charset=UTF-8',
                'Host': 'sportapi.fastball2.com',
                'Origin': 'https://test.f66b88sport.com',
                'Referer': 'https://test.f66b88sport.com/',
                'Sec-Fetch-Dest': 'empty',
                'Sec-Fetch-Mode': 'cors',
                'Sec-Fetch-Site': 'cross-site',
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
                'sec-ch-ua': '"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
                'sec-ch-ua-mobile': '?0',
                'sec-ch-ua-platform': '"Windows"',
            }
            # ss = requests.session()
            import urllib3
            # random.choice(IP_LIST)
            urllib3.disable_warnings()
            proxies = {'http': 'http://58.20.235.231:9002',
                       }
            # requests.adapters.DEFAULT_RETRIES = 5 proxies=proxies,
            # ss.proxies.update(proxies), proxies=proxies
            res = ss.post(url=url, json=json_data, headers=fb_headers, timeout=2, verify=False, stream=False)
            res_data = res.json()
            # print(res_data)
            ss.keep_alive = False
            ss.trust_env = False
            res.close()

            return res_data

        except Exception as e:
            mm += 1
            print('FB连接异常：', e)
            print("FB正在重新连接...")
            if mm == 2:
                return None


def ss_comm():
    headers1 = {
        # "Content-Type": "application/json;charset=utf-8",
        #
        #     'User-Agent': random.choice(USER_AGENT_LIST)
        'Accept': '*/*',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh,zh-CN;q=0.9',
        'Cookie': 'WEBSESSID=rdu68qkmgihiek2uaaricr8sm0; p0001_lang=zh_CN; p0001_token=dW5kZWZpbmVk; p0001_acc=%7B%22previewFlag%22%3Atrue%2C%22isDemo%22%3Afalse%2C%22spread%22%3A%22c%22%2C%22oddsType%22%3A%22hk%22%2C%22showDemo%22%3Afalse%2C%22showLogo%22%3Atrue%2C%22selectedMarket%22%3Anull%7D',
        'Referer': 'https://dywu.sp5178.com/whg/preview?langs=2',
        'Sec-Ch-Ua': '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
        'Sec-Ch-Ua-Mobile': '?0',
        'Sec-Ch-Ua-Platform': '"Windows"',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'same-origin',
        'Token': 'QVI=',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
        'X-Requested-With': 'XMLHttpRequest',
    }
    url = "https://dywu.sp5178.com/whg/sport-odds-preview/3?sprtid=N1&lang=2&spread=c&odd=hk&page=1&sorttyp=1&date=0&plytypgrp=1&gameMenuId=0"
    proxies = {'HTTP': "HTTPS://" + random.choice(IP_LIST)}
    req = requests.get(url, headers=headers1, timeout=2, proxies=proxies)
    try:
        req_data = req.json()
        # print(req_data)
        req.close()
        return req_data
    except Exception as e:
        # print(req.text)
        print(e)
    # finally:
    #     print(time.time()-t2)


def bc_conn():
    mm = 0
    while True:
        try:
            data_1 = {
                "command": "request_session",
                "params": {
                    "language": "zhh",
                    "site_id": "833",
                    "release_date": "20/03/2023-22:17"
                },
                "rid": int(round(time.time(), 4) * 10000)
            }
            hast = {'Accept-Encoding': 'gzip, deflate, br',
                    'Accept-Language': 'zh,zh-CN;q=0.9',
                    'Cache-Control': 'no-cache',
                    'Connection': 'Upgrade',
                    'Host': 'as-swarm-hongtubet.bcapps.net',
                    'Origin': 'https://sb-hk.bc.bangbanged.com',
                    'Pragma': 'no-cache',
                    'Sec-Websocket-Extensions': 'permessage-deflate; client_max_window_bits',
                    'Sec-Websocket-Key': 'ywC6+OGS3zc6etTWzwpntQ==',
                    'Sec-Websocket-Version': '13',
                    'Upgrade': 'websocket',
                    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36', }
            url = 'wss://as-swarm-hongtubet.bcapps.net/'
            ws = create_connection(url=url, timeout=2, headers=hast, sslopt={"cert_reqs": ssl.CERT_NONE})
            ws.send(json.dumps(data_1))
            ws.recv()
            return ws
        except Exception as e:
            mm += 1
            print('BB连接异常：', e)
            print("BB正在重新连接...")
            if mm == 5:
                print("退出重新连接")
                return None
            continue


def bti_conn(self, bisai_id):
    headers = {
        'Accept': 'application/json',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Authorization': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsYW5ndWFnZUNvZGUiOiJ6aCIsImJldHRpbmdWaWV3IjoiQXNpYW4gVmlldyIsInNvcnRpbmdUeXBlSWQiOjAsImJldHRpbmdMYXlvdXQiOjIsImRpc3BsYXlUeXBlSWQiOjEsInRpbWV6b25lSWQiOjIxLCJvZGRzU3R5bGVJZCI6IiIsImFsbG93Q2hhbmdlT2RkIjowLCJpbnRUYWJFeHBhbmRlZCI6MSwiY291bnRyeUNvZGUiOiJVQSIsImN1cnJlbmN5UmF0ZSI6MC4xMTU1MDU0MTc1NDgzMTIsImN1cnJlbmN5UmF0ZWV1ciI6MC4xMz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vbWVyTGV2ZWwiOjAsInNpdGVJZCI6MjAxMTYsInNlbGVjdGVkT3B0aW9uSWQiOjAsImlhdCI6MTY4Mzk3OTcxN30.aT6g0YQC9G4hKtWiAF1LGxe2yPq0j0N2FNtb7vr-5Ag; session=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjdXN0b21lcklkIjoxMTk4MzcyODAsImV4cGlyZWREYXRlIjoxNjg0MDY2MTIyMDkwLCJjdXN0b21lcklwIjoiMTE3LjE1MC4yMTIuMTI3IiwiaWF0IjoxNjgzOTc5NzE3fQ.MPWstV21PCWpbu6lnZAS_n2jtDo-awM5xOxfFkXWetc',
        'Referer': 'https://prod20116.ggg4x4.com/zh/asian-view/today/%E8%B6%B3%E7%90%83?operatorToken=NTRhNTFjNDAxMzkzMTk1NzQwYTFhMmU0ZjlkNDE1Yzc6YjYyOWIwYTVmMmI3ZGZhYmVlMjljYmVkYjA0MDI3OTYyNDNmMjExYjBiMTVlMDY3ZDY2ZTFjNWRjNGQwMWUyMDUzYWI1MWFlODQ0OTMxNzM2ODIwMTc5ZjlkZWE3YWMyNTQzZWE5YjVmMjE5OGU2ZGQ4NTdmNDRiNTVlM2ZhYTRkNGU1YzJmNzZkNzQxNDkxMmNhMzYyMDQ0MmI3YWFiYWFmYjZkMjIwZTI4OThmOGZlNWY1NTIwYzc4ZDYzZWEx',
        'Sec-Ch-Ua': '"Google Chrome";v="113", "Chromium";v="113", "Not-A.Brand";v="24"',
        'Sec-Ch-Ua-Mobile': '?0',
        'Sec-Ch-Ua-Platform': '"Windows"',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'same-origin',
        'Session': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjdXN0b21lcklkIjoxMTk4MzcyODAsImV4cGlyZWREYXRlIjoxNjg0MDY2MTIwNzQ4LCJjdXN0b21lcklwIjoiMTE3LjE1MC4yMTIuMTI3IiwiaWF0IjoxNjgzOTc5NzE3fQ.rwUdONZJzwGcoGh6J9zaANhLb9G_kbDGfb5iU2dhMv8',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36',

    }
    # url = "https://prod20217.bti-sports.io/api/eventpage/events/{}".format(bisai_id),httd
    url = 'https://prod20116.ggg4x4.com/api/eventpage/events/{}'.format(bisai_id)

    #  https://prod20217.bti-sports.io/api/eventpage/events/439839635037061120

    try:
        data = requests.get(url=url, headers=headers, timeout=2)
        # proxy = urllib3.ProxyManager('HTTP://{}'.format(random.choice(aaaip)))
        # req = httd.request('get', url=url, headers=headers, timeout=1)
        # bisai_data = json.loads(req.data.decode('utf-8'))["data"]
        dara = data.json()["data"]
        # print(bisai_data)
        data.close()

        self.bti_data(dara)

    except Exception as e:
        print(e)
        print('**************************************')
        return

    return dara


def lu_conn():
    mm = 0
    luurl = 'wss://as-swarm-ws-re.bcapps.net/'
    while True:
        try:
            #             data_1 = {
            #     "command": "request_session",
            #     "params": {
            #         "language": "zhh",
            #         "site_id": "510",
            #         "release_date": "17/09/2021-19:09"
            #     },
            #     "rid": int(round(time.time(), 4) * 10000)
            # }
            data_1 = {
                "command": "request_session",
                "params": {
                    "language": "zhh",
                    "site_id": 5592,
                    "release_date": "17/09/2021-19:09",
                    "source": 4,
                    "afec": "af4a4636501c47a9617d6e09f549ec62"
                },
                "rid": int(round(time.time(), 4) * 10000)
            }
            bb_headers = {
                'Host': 'as-swarm-ws-re.bcapps.net',
                'Connection': 'Upgrade',
                'Pragma': 'no-cache',
                'Cache-Control': 'no-cache',
                # 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0',
                'Upgrade': 'websocket',
                'Origin': 'https://m-hk.luckysports.bangbanged.com',
                'Sec-WebSocket-Version': '13',
                'Accept-Encoding': 'gzip, deflate, br, zstd',
                'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
                'Sec-WebSocket-Key': 'FYZAWYIAv6tsoCZQXX580g==',
                'Sec-WebSocket-Extensions': 'permessage-deflate; client_max_window_bits',

                "User-Agent": random.choice(USER_AGENT_LIST),
            }

            data_2 = {
                "command": "request_session",
                "params": {
                    "language": "zhh",
                    "site_id": 5592,
                    "release_date": "17/09/2021-19:09",
                    "source": 4,
                    "afec": "ade3c296b68c448fda4a75061b74ad34"
                },
                "rid": int(round(time.time(), 4) * 10000)
            }
            he = {
                'Host': 'as-swarm-ws-re.bcapps.net',
                'Connection': 'Upgrade',
                'Pragma': 'no-cache',
                'Cache-Control': 'no-cache',
                'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36',
                'Upgrade': 'websocket',
                'Origin': 'https://m-hk.luckysports.bangbanged.com',
                'Sec-WebSocket-Version': '13',
                'Accept-Encoding': 'gzip, deflate, br, zstd',
                'Accept-Language': 'zh-CN,zh;q=0.9',
                'Sec-WebSocket-Key': '2cAZrU7ebfypWXiJ2/hcqg==',
                'Sec-WebSocket-Extensions': 'permessage-deflate; client_max_window_bits',
            }
            # proxies = {'http': random.choice(IP_LIST)}  # , proxies=proxies
            # ws = create_connection(url=luurl, timeout=1, headers=bb_headers, sslopt={"cert_reqs": ssl.CERT_NONE})
            ws = create_connection(url=luurl, timeout=1, headers=bb_headers, sslopt={"cert_reqs": ssl.CERT_NONE})
            ws.send(json.dumps(data_2))
            # print()
            ws.recv()
            return ws
        except Exception as e:
            mm += 1
            print('LU连接异常：', e)
            print("LU正在重新连接...")
            # if mm == 5:
            #     print("退出重新连接")
            #     return None
            continue


# lu_conn()

def db_uid(requestId):
    db_headers = {
        'Accept': 'application/json, text/plain, */*',
        'Accept-Encoding': 'gzip, deflate, br, zstd',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'Connection': 'keep-alive',
        # 'Host': 'api.8pb466ai.com',
        # 'Origin': 'https://sports-pc.ggsui.com',
        # 'Referer': 'https://sports-pc.ggsui.com/',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'cross-site',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0',
        # 'checkId': 'pc-c408735d350b146395c218fc0677963aa5392d3b-520801638825930004-1736769822776',
        'lang': 'zh',
        'request-code': '{"panda-bss-source":"2"}',
        'requestId': requestId,
        'sec-ch-ua': '"Microsoft Edge";v="131", "Chromium";v="131", "Not_A Brand";v="24"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
    }
    t1 = int(time.time() * 1000)
    # uid
    url = f'https://api.dbsportxxx278gwf4.com/yewu12/user/getUserParamConfig?t={t1}'
    da = requests.post(url=url, headers=db_headers, json={"paramKey": ""})
    # print(da.json())
    try:
        uid = da.json()['data'][0]['uid']
        print(uid)
        return uid
    except Exception as e:
        print(e)
        msg = da.json()['msg']
        print(msg)
        if msg == '账户信息已过期,请重新登录':
            db_token()

def db_conn(uid, requestId):
    db_headers = {
        'Accept': 'application/json, text/plain, */*',
        'Accept-Encoding': 'gzip, deflate, br, zstd',
        'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
        'Connection': 'keep-alive',
        # 'Host': 'api.8pb466ai.com',
        # 'Origin': 'https://sports-pc.ggsui.com',
        # 'Referer': 'https://sports-pc.ggsui.com/',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'cross-site',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36 Edg/131.0.0.0',
        # 'checkId': 'pc-c408735d350b146395c218fc0677963aa5392d3b-520801638825930004-1736769822776',
        'lang': 'zh',
        'request-code': '{"panda-bss-source":"2"}',
        'requestId': requestId,
        'sec-ch-ua': '"Microsoft Edge";v="131", "Chromium";v="131", "Not_A Brand";v="24"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
    }
    t2 = int(time.time() * 1000)
    url2 = f'https://api.dbsportxxx278gwf4.com/yewu11/v1/w/structureMatchesPB?t={t2}'
    json_data = {
        "cuid": uid,
        "sort": 1,
        "tid": "",
        "cpn": 1,
        "cps": 500,
        "apiType": 1,
        "euid": "30002",
        "orpt": "-1"
    }
    da = requests.post(url=url2, headers=db_headers, json=json_data, timeout=3)
    # Base64 解码
    try:
        data = da.json()['data']
    except Exception as e:
        print("01")
        time.sleep(2)
        t2 = int(time.time() * 1000)
        url2 = f'https://api.dbsportxxx278gwf4.com/yewu11/v1/w/structureMatchesPB?t={t2}'
        da = requests.post(url=url2, headers=db_headers, json=json_data, timeout=3)
        data = da.json()['data']
    comp_data = base64.b64decode(data)
    # Gzip 解压
    with gzip.GzipFile(fileobj=io.BytesIO(comp_data)) as f:
        decom_data = f.read()
    # 打印解压后的数据
    data_data = decom_data.decode('utf-8')
    # print(data_data)
    # open('a1.json', 'w',encoding='utf-8').write(data_data)
    return data_data
    #

def db_token():
    headers_1 = {
        'Accept': 'application/json, text/plain, */*',
        'Accept-Encoding': 'gzip, deflate, br, zstd',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Connection': 'keep-alive',
        # 'Host': 'api.dbsporxxxw1box.com',
        # 'Origin': 'https://www.dbgaming.com',
        # 'Referer': 'https://www.dbgaming.com/',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'cross-site',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
        'sec-ch-ua': '"Google Chrome";v="131", "Chromium";v="131", "Not_A Brand";v="24"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
    }
    url_2 = 'https://api.dbsporxxxw1box.com/yewu6/user/tryPlay?lang=zh&terminal=PC'
    pp = requests.get(url_2, headers=headers_1)
    token = pp.json()['data']['token']
    print(token)
    sql = "UPDATE db_token SET token = %s"
    sql_name = Mysql_data()
    sql_name.run_sql(sql, token)

# import sys
# import os
# print(sys.executable,os.path.abspath(__file__))